﻿using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BikiBall.Input
{
    public class InputEngine
    {
        private Motion _motion = null;
        static public bool IsTapped { get; private set; }
        static public bool IsHold { get; set; }
        public float PinchDelta = 1.0f;
        private float _calibredYaw = 0;
        private float _calibredPitch = 0;
        private float _calibredRoll = 0; //0.30f;
        public float Yaw
        {
            get
            {
                return _motion.CurrentValue.Attitude.Yaw - _calibredYaw;
            }
        }
        public float Pitch
        {
            get
            {
                return _motion.CurrentValue.Attitude.Pitch - _calibredPitch;
            }
        }

        public float Roll
        {
            get
            {
                return -_motion.CurrentValue.Attitude.Roll - _calibredRoll;
            }
        }


        public InputEngine()
        {
            this._motion = new Motion();
            IsTapped = false;
            IsHold = false;
            TouchPanel.EnabledGestures = GestureType.Tap | GestureType.Pinch | GestureType.PinchComplete | GestureType.Hold;
        }

        public void Update()
        {
            //IsTapped = false;
            //IsHold = false;
            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();
                switch (gesture.GestureType)
                {
                    case GestureType.Tap:
                        {
                            IsTapped = !IsTapped;
                            /*this._calibredPitch = _motion.CurrentValue.Attitude.Pitch;
                            this._calibredYaw = _motion.CurrentValue.Attitude.Yaw;
                            this._calibredRoll = -_motion.CurrentValue.Attitude.Roll;*/
                            break;
                        }
                    case GestureType.Pinch:
                        {
                            float oldist = Vector2.Distance(gesture.Position - gesture.Delta, gesture.Position2 - gesture.Delta2);
                            float newdist = Vector2.Distance(gesture.Position, gesture.Position2);
                            this.PinchDelta *= (oldist / newdist);
                            break;
                        }
                    case GestureType.PinchComplete:
                        {
                            break;
                        }
                    case GestureType.Hold:
                        {
                            IsHold = !IsHold;

                            break;
                        }
                    default:
                        break;
                }
            }
        }

        public void Start()
        {
            this._motion.Start();
        }

        public void Stop()
        {
            this._motion.Stop();
        }
    }
}
